package com.dyrj.ysk.controller;

import com.dyrj.ysk.configuration.SdkProperties;
import com.dyrj.ysk.exception.BizException;
import com.dyrj.ysk.service.SignService;
import com.dyrj.ysk.util.CusAccessObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 通知控制器
 * @author 吴侠志  2019/10/19
 * @version 1.0
 */
@Controller
@Slf4j
@RequestMapping("/ysksdk")
public class NotifyController {
    @Autowired
    private SdkProperties sdkProperties;
    @Autowired
    private SignService signService;
    /**
     * CLOUDPAY6004
     * 商户支付前台通知页面示例
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/jumpUrl",method = RequestMethod.POST)
    public String jumpUrl(HttpServletRequest request, Model model) throws IOException, BizException {
        try{
            Map<String,String> params=CusAccessObjectUtil.getAllRequestParam(request);
            log.info(params.toString());
            model.addAttribute("params", params);
            //验签
            String sign=params.remove("Signature");
            signService.verify(params,sign,sdkProperties.getPublicKey());
        }catch (Exception e){
            e.printStackTrace();
        }
        return "ysksdk/jumpUrl";
    }

    /**
     * CLOUDPAY6005
     * 商户支付后台通知地址
     * @param request
     * @param response
     * @throws IOException
     * @throws BizException
     */
    @RequestMapping(value = "/notifyUrl",method = RequestMethod.POST)
    @ResponseBody
    public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, BizException {
        Map<String,String> params=CusAccessObjectUtil.getAllRequestParam(request);
        log.info(params.toString());
        //验签
        String sign=params.remove("Signature");
        boolean verify=signService.verify(params,sign,sdkProperties.getPublicKey());
        System.out.println("验签结果："+verify);
        response.addHeader("Content-Type", "text/html;charset=utf-8");
        response.getWriter().write("notify_success");
    }

    /**
     * CLOUDPAY6006
     * 商户绑卡前台通知页面示例
     * @param request
     * @param model
     * @return
     * @throws IOException
     * @throws BizException
     */
    @RequestMapping(value = "/bindCardJumpUrl",method = RequestMethod.POST)
    public String bindCardJumpUrl(HttpServletRequest request, Model model) throws IOException, BizException {
        Map<String,String> params=CusAccessObjectUtil.getAllRequestParam(request);
        log.info(params.toString());
        model.addAttribute("params", params);
        //验签
        String sign=params.remove("Signature");
        signService.verify(params,sign,sdkProperties.getPublicKey());
        return "ysksdk/jumpUrl";
    }

    /**
     * CLOUDPAY6007
     * 商户绑卡后台通知
     * @param request
     * @param response
     * @throws IOException
     * @throws BizException
     */
    @RequestMapping(value = "/bindCardNotifyUrl",method = RequestMethod.POST)
    @ResponseBody
    public void bindCardNotifyUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, BizException {
        Map<String,String> params=CusAccessObjectUtil.getAllRequestParam(request);
        log.info(params.toString());
        //验签
        String sign=params.remove("Signature");
        signService.verify(params,sign,sdkProperties.getPublicKey());
        response.addHeader("Content-Type", "text/html;charset=utf-8");
        response.getWriter().write("notify_success");
    }

}
